#include "define.h"
#include "msocket.h"

void makesockaddr(struct sockaddr_in *addr,const char *ip,const short port)
{
	addr->sin_family=AF_INET;
	addr->sin_port = htons(port);
	addr->sin_addr.s_addr = inet_addr(ip);
	memset(&(addr->sin_zero), 0x00, 8);
}

int open_listen(const short port)
{
	int sockfd;
	struct sockaddr_in my_addr;
	PCHECK(sockfd=socket( AF_INET,SOCK_STREAM, 0 ), "socket" );
	my_addr.sin_family=AF_INET;
	my_addr.sin_port=htons(port);
	my_addr.sin_addr.s_addr = INADDR_ANY;
	memset( &(my_addr.sin_zero),0x00,8 );
	PCHECK(bind(sockfd,(struct sockaddr *)&my_addr, sizeof(struct sockaddr)) ,"bind" );
	PCHECK(listen(sockfd,BACKLOG), "listen" );
	return sockfd;
}

int connect_to(const char *ip, const short port)
{
	int sfd;
	struct sockaddr_in maddr;

	memset( &maddr, 0x00, sizeof(struct sockaddr_in) );
	makesockaddr( &maddr, ip, port );
	PCHECK(sfd=socket( AF_INET,SOCK_STREAM, 0 ), "socket" );
	PCHECK(connect( sfd, (struct sockaddr *)&maddr, sizeof(struct sockaddr)), "connect" );

	return sfd;
}

